package com.zhiyou100.cudr;

import com.zhiyou100.dao.StudentDao;
import com.zhiyou100.entityclass.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @packageName: ${NAME}
 * @className: ${PACKAGE_NAME}
 * @Description: TODO 获取一个学生
 * @author: yang
 * @date: 7/22/20
 */
@WebServlet(name = "GetOne", urlPatterns = "/getOne")
public class GetOne extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        // 设定编码集
        response.setContentType("text/html;charset=UTF-8");
        // 设定类型

        // new StudentDao
        StudentDao studentDao = new StudentDao();
        // 获取参数
        String sid = request.getParameter("sid");

        if (new Student().sidFlag(sid)){
            // 合理
            // 获取一个
            Student student = studentDao.getOne(Integer.parseInt(sid));
            // 放入 Session 中
            request.getSession().setAttribute("student", student);
            System.out.println(sid+"        "+student);
            // 转发
            response.sendRedirect(request.getContextPath() + "/jsp/getOne.jsp");
        }else {
            // 不合理
            request.getSession().setAttribute("student", null);

            // 转发
            response.sendRedirect(request.getContextPath() + "/jsp/getOne.jsp");
        }



    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
